<?php

namespace backend\controllers;

use Yii;
use common\models\Invitation\Invitation;
use common\models\Invitation\InvitationSearch;
use common\models\Expert\Expert;
use common\models\Expert\ExpertSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\adminlog\AdminLog;
use zhongkefu\zhongkefu;
use zhongkefu\helpers\Utils;
use dosamigos\qrcode\QrCode;
use zhongkefu\helpers\SwfiEmail;
/**
 * InvitationController implements the CRUD actions for Invitation model.
 */
class InvitationController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Invitation models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new InvitationSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Invitation model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Invitation model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Invitation();
        if ($model->load(Yii::$app->request->post())) {
                $email = Expert::find()->where(['id' => $model->email])->asArray()->one()['email'];
               // var_dump($email);die();
                //报名成功发送邮件
                $mailer = SwfiEmail::getEmail();
                $mail= $mailer->compose();
                $mail->setTo($email);
                $xinxi = $model->xinxi;
                $xinxi = '<p><span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"></span></p>
                <p><span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"><br/></span></p>
                <p><span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"></span></p>
                <p style="text-align: center;"><span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;"><a href=" ">'.$xinxi.'</a></span>
                </p>
                <p style="text-align: right;"><br/></p>
                <hr/>
                <p style="text-align: right;"><span style="font-family: 微软雅黑, &#39;Microsoft YaHei&#39;; font-size: 14px;">&nbsp; &nbsp;| &nbsp;由协会管理系统的技术支持.&nbsp;</span><br/></p>';
                $mail->setHtmlBody($xinxi);//发布可以带html标签的文本
                if($mail->send()){
                    Yii::$app->session->setFlash('success', '邮件发送成功！');

                    if ($model->save()) {
                         return $this->redirect(['view', 'id' => $model->id]);
                    }
                }else{
                    Yii::$app->session->setFlash('error','邮件发送失败！');
                }
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Invitation model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Invitation model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Invitation model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Invitation the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Invitation::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

        /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
        $artname="";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);

            foreach($ids as $val){
                $models = Invitation::findOne(['id' => $val]);
                $artname .= $models->eid.",";
            }
            $artname = substr($artname,0,-1);
            
            foreach($ids as $id){
                $model = Invitation::findOne(['id' => $id]);
                $this->findModel($id)->delete();

            }

            Adminlog::addlog(yii::$app->user->id, '批量删除活动 '.$artname);
            return true;
        }
    }

}
